package com.hl.sfzy.service.wxwappService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.wxwapp.TeacherDetail;
import com.hl.sfzy.entity.wxwapp.User;
import com.hl.sfzy.entity.wxwapp.WXWConsultwait;
import com.hl.sfzy.entity.wxwapp.WXWConsultwaitName;
import com.hl.sfzy.mapper.wxwappMapper.WXWConsultwaitMapper;
import com.hl.sfzy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className: WXWConsultWait
 * @author: WXW
 * @date: 2024/9/11 22:56
 * @Version: 1.0
 * @description:
 */
@Service
public class WXWConsultWaitService {
    @Autowired
    private WXWConsultwaitMapper wXWConsultwaitMapper;

    //    查询所有的预约信息
    public R findAllReservation(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<WXWConsultwaitName> wxwConsultwaitNames = wXWConsultwaitMapper.selectReservation();
        PageInfo<WXWConsultwaitName> pageInfo = new PageInfo<>(wxwConsultwaitNames);
        return R.success(pageInfo);
    }

    //    查询所有的咨询师的id和name
    public R findAllTeacherIdAndName() {
        List<TeacherDetail> wxwConsultwaitNames = wXWConsultwaitMapper.selectTeacherIdAndName();
        return R.success(wxwConsultwaitNames);
    }

    //    查询所有的User的id和name
    public R findAllUserIdAndName() {
        List<User> Users = wXWConsultwaitMapper.selectUserIdAndName();
        return R.success(Users);
    }

    //新增预约详情
    public R addReservation(WXWConsultwait wXWConsultwait) {
        wXWConsultwaitMapper.insert(wXWConsultwait);
        return R.success();
    }

    //通过id查询预约详情（名字的）
    public R findReservationById(Integer id) {
        WXWConsultwaitName wxwConsultwaitName = wXWConsultwaitMapper.selectReservationById(id);
        return R.success(wxwConsultwaitName);
    }

    //    修改
    public R updateReservation(WXWConsultwaitName wXWConsultwaitName) {
        //通过姓名查询教师id
        int teacherId = wXWConsultwaitMapper.selectTeacherIdByName(wXWConsultwaitName.getTeacherName());
        //通过姓名查询User的id
        int userId = wXWConsultwaitMapper.selectUserIdByName(wXWConsultwaitName.getUserName());
        WXWConsultwait wxwConsultwait = new WXWConsultwait();
        wxwConsultwait.setId(wXWConsultwaitName.getId());
        wxwConsultwait.setType(wXWConsultwaitName.getType());
        wxwConsultwait.setCreatetime(wXWConsultwaitName.getCreatetime());
        wxwConsultwait.setContent(wXWConsultwaitName.getContent());
        wxwConsultwait.setStatus(wXWConsultwaitName.getStatus());
        wxwConsultwait.setUserId((long) userId);
        wxwConsultwait.setTeacherId((long) teacherId);

        int i = wXWConsultwaitMapper.updateByPrimaryKey(wxwConsultwait);
        return R.success(i);
    }

    //删除
    public R deleteReservation(Integer id) {
        int i = wXWConsultwaitMapper.deleteByPrimaryKey((long)id);
        return R.success(i);
    }


    //根据咨询师姓名模糊查询
    public R findReservationByTeacherName(String teacherName,Integer page,Integer size) {
        System.out.println("teacherName = " + teacherName);
        List<Integer> integers = wXWConsultwaitMapper.selectTeacherIdByNameLike(teacherName);
        System.out.println("integers = " + integers);
        PageHelper.startPage(page,size);
        List<WXWConsultwaitName> wxwConsultwaitNames = wXWConsultwaitMapper.selectReservationByName(integers);
        PageInfo<WXWConsultwaitName> pageInfo = new PageInfo<>(wxwConsultwaitNames);
        return R.success(pageInfo);
    }
}
